//
//  YHNSFileManager.m
//  SCOffers
//
//  Created by kevin on 14-10-16.
//  Copyright (c) 2014年 kevin. All rights reserved.
//

#import "YBKFileManager.h"

@implementation YBKFileManager
#pragma mark - 根目录
+(NSString*)rootDirectoryPath:(FileDirectoryType)type{
    NSString *filePath  = nil;
    if(type  == FILEDIRECTORY_TYPE_HOME){
        filePath  = NSHomeDirectory();
    }else if(type == FILEDIRECTORY_TYPE_DOCUMENT){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        filePath = [paths objectAtIndex:0];
    }else if(type == FILEDIRECTORY_TYPE_CACHE){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        filePath = [paths objectAtIndex:0];
    }else if(type  == FILEDIRECTORY_TYPE_LIBRARY){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        filePath = [paths objectAtIndex:0];
    }else if (type == FILEDIRECTORY_TYPE_TMP){
        filePath = NSTemporaryDirectory();
    }
    return filePath;
}
+(NSString*)filePath:(FileDirectoryType)type directoryName:(NSString *)dirName fileName:(NSString *)name{
    //根目录路径
    NSString *rootPath  = [self rootDirectoryPath:type];
    NSString *tmpFileName = nil;
    if(dirName){
        tmpFileName = [[rootPath stringByAppendingPathComponent:dirName] stringByAppendingPathComponent:name];
    }else{
        tmpFileName = [rootPath stringByAppendingPathComponent:name];
    }
    return tmpFileName;
}
#pragma mark - 创建文件夹
+(BOOL)createFileDirectory:(FileDirectoryType)type directoryName:(NSString *)name{
    NSString *filePath  = [self rootDirectoryPath:type];
    if(filePath == nil || filePath.length == 0){
        return NO; //没有目录
    }
    filePath = [filePath stringByAppendingFormat:@"/%@",name];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isDirExist = [fileManager fileExistsAtPath:filePath isDirectory:&isDir];
    if(!(isDirExist && isDir))    {
        BOOL bCreateDir = [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bCreateDir){
            NSLog(@"Create Directory Failed.");
            return NO;
        }
    }
    return YES;
}
#pragma mark - 创建文件
+(BOOL)createFile:(FileDirectoryType)type childDirectoryName:(NSString *)directoryName fileName:(NSString *)name{
    //根目录
    NSString *rootPath = [self rootDirectoryPath:type];
    //文件路径
    NSString *filePath = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(directoryName == nil){
        //在根目录下面创建文件
        filePath = [rootPath stringByAppendingPathComponent:name];
    }else{
        BOOL isCreateDir = [self createFileDirectory:type directoryName:directoryName];
        if(!isCreateDir) return NO; //创建失败
        filePath = [[filePath stringByAppendingPathComponent:directoryName] stringByAppendingPathComponent:name];
    }
    if(![fileManager fileExistsAtPath:filePath]){
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    }
    return YES; 
    
}
#pragma mark 写文件
+(BOOL)writeFileData:(FileDirectoryType)dirType directoryName:(NSString *)dirName fileName:(NSString *)fName fileDataType:(FileDataType)type dataObject:(id)object{
    NSString *filePath = [self filePath:dirType directoryName:dirName fileName:fName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:filePath])
    {
        //创建
        BOOL isCreateFile = [self createFile:dirType childDirectoryName:dirName fileName:fName];
        if(!isCreateFile){
            return NO; //创建失败
        }
    }
    if(object == nil) return NO;
    NSData *data =nil;
    if([object isKindOfClass:[NSData class]]){
        data =object;
    }
    else {
        data = [NSKeyedArchiver archivedDataWithRootObject:object];
    }
    [data writeToFile:filePath atomically:YES];
    return NO;
}
#pragma mark - 读取文件
+(id)readFileData:(FileDirectoryType)dirType directoryName:(NSString *)dirName fileName:(NSString *)fName fileDataType:(FileDataType)type{
    NSString *filePath = [self filePath:dirType directoryName:dirName fileName:fName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:filePath])
    {
        //创建
        BOOL isCreateFile = [self createFile:dirType childDirectoryName:dirName fileName:fName];
        if(!isCreateFile){
            return nil; //创建失败
        }
    }
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
+(id)readmainBundleFileData:(NSString *)fileName fileDataType:(FileDataType)type{
    NSArray *fileArray = [fileName componentsSeparatedByString:@"."];
    if(fileArray.count<2){
        return nil;
    }
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:[fileArray objectAtIndex:0]  ofType:[fileArray objectAtIndex:1]];
    if(type  == FILEDATA_TYPE_ARRAY){
        return [NSArray arrayWithContentsOfFile:plistPath];
    }else{
        return [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    }
}
@end
